// pages/personcenter/editmessage/editmessage.js
const app = getApp()
import WeCropper from '../../dist/we-cropper.js'
const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 100;
Page({
  data: {
    headurl: '',
    username: '',
    mark: '',
    markval: '',
    tel: '',
    telval: '',
    hiddenmodalput: true,
    hiddenmodalput1: true,
    yzm: '',
    time: '获取验证码', //倒计时 
    currentTime: 60, //限制60s
    isClick: false, //获取验证码按钮，默认允许点击
    cropperOpt: { //裁剪基础设置 
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    },
  },

  onLoad: function(options) {
    this.setData({
      headurl: options.headurl,
      username: options.username,
      mark: options.mark,
      tel: options.tel
    })

    const { //裁剪
      cropperOpt
    } = this.data
    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {})
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas();
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    var that = this;
    that.wecropper.getCropperImage((src) => {
      console.log(src)
      if (src) {
        //此处添加用户确定裁剪后执行的操作 src是截取到的图片路径
        console.log(src)
        that.setData({
          xx: false,
          img: src,
        })
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.globalData.url + '/jxb/login/login/uploadAvatar', //仅为示例，非真实的接口地址
          filePath: src,
          name: 'files',
          formData: {
            'user': 'test'
          },
          success(res) {
            let data = JSON.parse(res.data)
            console.log(res.data)

            that.setData({
              headurl: data.data.url,
            })
            that.saveinfo('', '', data.data.url);
          }
        })
      }
    })
  },
  changehead() { //更换头像
    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        that.setData({
          cutImage: 'show',
          addtribeConShow: 'hide',
          xx: true
        });
        that.wecropper.pushOrign(res.tempFilePaths[0]);
        // wx.uploadFile({
        //   url: app.globalData.url + '/jxb/login/login/uploadAvatar', //仅为示例，非真实的接口地址
        //   filePath: tempFilePaths[0],
        //   name: 'files',
        //   formData: {
        //     'user': 'test'
        //   },
        //   success(res) {
        //     let data = JSON.parse(res.data)
        //     console.log(res.data)

        //     that.setData({
        //       headurl: data.data.url,
        //     })
        //     that.saveinfo('', '', data.data.url);
        //   }
        // })
      }
    })
  },
  editmark() { //修改备注
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  edittel() { //修改电话号码
    this.setData({
      hiddenmodalput1: !this.data.hiddenmodalput1
    })
  },
  cancel: function() {
    this.setData({
      hiddenmodalput: true,
      hiddenmodalput1: true
    });
  },
  //确认  
  confirm: function() {
    this.setData({
      hiddenmodalput: true,
      mark: this.data.markval
    })
    this.saveinfo(this.data.markval, '', '');
  },
  confirm1() { //提交手机号码
    this.setData({
      hiddenmodalpu1: true,
    })
    this.checkcode();
    // if(this.check()){
    //   this.saveinfo('', this.data.tel, '');
    // }

  },
  nameinp(e) { //获取姓名
    this.setData({
      markval: e.detail.value
    })
  },
  getyzm() { //修改验证码
    if (this.check()) {
      let that = this;
      that.setData({
        isClick: true,
      })
      var currentTime = that.data.currentTime;
      var interval = setInterval(function() {
        currentTime--; //减
        that.setData({
          time: currentTime + '秒后获取'
        })
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            time: '获取验证码',
            currentTime: 60,
            isClick: false
          })
        }
      }, 1000);
      wx.request({
        url: app.globalData.url + '/jxb/login/login/getCode',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          tel: this.data.tel
        },
        success: function(res) {
          that.setData({
            yzmcode: res.data.data,
          })
          console.log(res.data)
        },

      })

    }
    // this.setData({ yzm: event.detail.value })
  },
  yzm(e) { //填写验证码
    console.log(e.detail.value)
    this.setData({
      yzm: e.detail.value
    })
  },
  checkcode(yzmcode) {
    let that = this;
    wx.request({
      url: app.globalData.url + '/jxb/login/login/codeExpired',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        tel: this.data.tel,
        code: this.data.yzm,
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.saveinfo('', that.data.tel, '')
          that.cancel();
        } else if (res.data.code == 402) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },

    })
  },
  tel(e) { //修改手机
    this.setData({
      tel: e.detail.value
    })
  },
  saveinfo(remark, tel, avatar) { //保存信息
    wx.request({
      url: app.globalData.url + '/jxb/oneself/updateUserInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        remark: remark,
        tel: tel,
        avatar: avatar,
      },
      success: function(res) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      },

    })
  },
  esclogin() { //退出登录
    wx.request({
      url: app.globalData.url + '/jxb/login/loginQuit',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
      },
      success: function(res) {
        wx.showToast({
          title: '退出成功',
          icon: 'success',
          duration: 2000
        })
        wx.removeStorageSync('token')
        wx.removeStorageSync('uid');
        wx.clearStorage()
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
          headurl: '',
          username: '',
          mark: '',
          tellogin: '',

        })
        setTimeout(function() {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }, 2000)

      },

    })
  },
  check() {
    if (this.data.tel == '') {
      wx.showToast({
        title: '请填写您的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      if (!(/^1[345789]\d{9}$/.test(this.data.tel))) {
        wx.showToast({
          title: '手机格式有误',
          icon: 'none',
          duration: 2000
        });
        return false;
      } else {}
    }
    return true;
  }
})